﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using ClassLibrary.Utilities;

public partial class Admin_UserControl_ucLogin : System.Web.UI.UserControl
{
    TblUserAccountDAO userDAO = new TblUserAccountDAO(Global.CONNECTION_STRING);
    HttpCookie cookie_username = null;
    HttpCookie cookie_pass = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Cookies["pass"] != null && Request.Cookies["username"] != null)
            {
                txtUserName.Text = Request.Cookies["username"].Value;
                txtPassword.Text = Request.Cookies["pass"].Value;
            }
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        bool bFlag = false;
        try
        {
            // khai báo biến cookie
            cookie_username = new HttpCookie("username", txtUserName.Text);
            cookie_pass = new HttpCookie("pass", txtPassword.Text);

            // Gan ngay song cho cookie
            cookie_username.Expires = DateTime.Now.AddDays(30);
            cookie_pass.Expires = DateTime.Now.AddDays(30);

            //Them cookie

            if (chkRemember.Checked)
            {
                cookie_username.Value = txtUserName.Text;
                cookie_pass.Value = txtPassword.Text;
                Response.Cookies.Add(cookie_username);
                Response.Cookies.Add(cookie_pass);
            }
            else
            {
                cookie_username.Value = null;
                cookie_pass.Value = null;
            }
            if (txtUserName.Text != "" && txtPassword.Text != "")
            {

                TblUserAccount userAccount = userDAO.GetAdmin(txtUserName.Text.Trim(), Encryption.EncryptToString(txtPassword.Text));
                if (userAccount != null)
                {

                    if (userAccount.AccountStatus > 0)
                    {
                        Session[Global.SESSION_ADMIN_USER] = userAccount;
                        bFlag = true;
                    }
                    else
                    {
                        lbmessage.Text = "Tên đăng nhập ( mật khẩu ) chưa trùng khớp .";
                        lbmessage.Visible = true;
                    }

                }
            }
            else
            {
                lbmessage.Text = "Tên đăng nhập không tồn tại.";
                lbmessage.Visible = true;
            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (bFlag)
            Response.Redirect("~/Admin/DanhSachSanPham.aspx");
    }
}
